## Copyright 2010 Sindwiller ( sindwiller ayat gmail doooot com )

##    This file is part of rrabboap.
##
##    Foobar is free software: you can redistribute it and/or modify
##    it under the terms of the GNU General Public License as published by
##    the Free Software Foundation, either version 3 of the License, or
##    (at your option) any later version.
##
##    Foobar is distributed in the hope that it will be useful,
##    but WITHOUT ANY WARRANTY; without even the implied warranty of
##    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##    GNU General Public License for more details.
##
##    You should have received a copy of the GNU General Public License
##    along with Foobar.  If not, see <http://www.gnu.org/licenses/>.


import math

class Vector2D(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __add__(self, v):
        return Vector2D(float(self.x + v.x), float(self.y + v.y))

    def __sub__(self, v):
        return Vector2D(float(self.x - v.x), float(self.y - v.y))

    def __str__(self):
        return '(%d, %d)' % (self.x, self.y)

    def __mul__(self, other):
        if isinstance(other, int) or isinstance(other, float):
            return Vector2D(float(self.x * other), float(self.y * other))
        ## Dot product, not cross product!
        elif isinstance(other, Vector2D):
            return float((self.x * other.x) + (self.y * other.y))
        else:
            raise Exception, "Tried to multiply a vector with a non-int/non-float or a non-vector."

    def __lt__(self, v):
        lengthSelf = float(math.sqrt((self.x * self.x) + (self.y * self.y)))
        if isinstance(v, Vector2D):
            lengthV = float(math.sqrt((v.x * v.x) + (v.y * v.y)))
        else:
            lengthV = v
        if lengthSelf < lengthV:
            return True
        else:
            return False
    def __eq__(self, v):
        lengthSelf = float(math.sqrt((self.x * self.x) + (self.y * self.y)))
        if isinstance(v, Vector2D):
            lengthV = float(math.sqrt((v.x * v.x) + (v.y * v.y)))
        else:
            lengthV = v
        if lengthSelf == lengthV:
            return True
        else:
            return False
    def __gt__(self, v):
        lengthSelf = float(math.sqrt((self.x * self.x) + (self.y * self.y)))
        if isinstance(v, Vector2D):
            lengthV = float(math.sqrt((v.x * v.x) + (v.y * v.y)))
        else:
            lengthV = v
        if lengthSelf > lengthV:
            return True
        else:
            return False

    def length(self):
        return float(math.sqrt((self.x * self.x) + (self.y * self.y)))

    def angle(self, v):
        return math.acos(float((self.x * v.x) + (self.y * v.y)))

    def normalize(self):
        length = float(math.sqrt((self.x * self.x) + (self.y * self.y)))
        return Vector2D(float(self.x / length), float(self.y / length))


##if __name__ == '__main__':
##    ## Unit test
##
##    v1 = Vector2D(10, 10)
##    v2 = Vector2D(-5, -5)
##    v3 = v1 + v2
##    dot_product = v1 * v2
##    angle = v1.angle(v2)
##    print v3
##    print dot_product
##    print angle
